<%@ Page debug="true" Trace="false" %>

<%@ Import Namespace="System.Diagnostics" %>
<%@ Import Namespace="WroxTest" %>

<%@ Register TagPrefix="wrox" TagName="Heading" src="..\Controls\Heading.ascx" %>
<%@ Register TagPrefix="wrox" TagName="Footing" src="..\Controls\Footing.ascx" %>

<html>
<head>
<wrox:Heading Title="Performance Counters" runat="server"/>
<!--------------------------------------------------------------------------->

<html>

<script language="VB" runat="server">

	Dim objectName As String = "Wrox"
	
  Sub Page_Load(Sender As Object, e as EventArgs)

    Dim cat As PerformanceCounterCategory
    Dim pc  As PerformanceCounter
    Dim cs1 As CounterSample
    Dim cs2 As CounterSample
    
    If Not PerformanceCounterCategory.Exists(objectName)
    	Response.Write("Category does not exist")
    	Response.End()
    End If
    
    cat = New PerformanceCounterCategory(objectName)
    pc = New PerformanceCounter
    Response.Write("<table border='1'><thead><th>Name</th><th>Type</th><th>RawValue</th><th>NextValue</th><th>Sample</th></thead>")
    For Each pc In cat.GetCounters()
      Response.Write("<tr><td>" & pc.CounterName & "</td>" & _
                         "<td>" & CounterTypeDesc(pc.CounterType) & "</td>" & _
                         "<td>" & pc.RawValue    & "</td>" & _
                         "<td>" & pc.NextValue() & "</td>")
      Response.Write("<td><table><tr>")
      cs1 = pc.NextSample()
      Response.Write("<td>Base Value=" & cs1.BaseValue & "<br/>" & _
                         "Counter Frequency=" & cs1.CounterFrequency & "<br/>" & _
                         "Counter Type=" & CounterTypeDesc(cs1.CounterType) & "<br/>" & _
                         "Raw Value=" & cs1.RawValue & "<br/>" & _
                         "System Frequency=" & cs1.SystemFrequency & "<br/>" & _
                         "Time Stamp=" & cs1.TimeStamp & "<br/>" & _
                         "Time Stamp 100n Sec=" & cs1.TimeStamp100nSec & "</td>")
      'Dim wfs as new WaitForASec
      'wfs.Wait(5000)
      cs2 = pc.NextSample()
      Response.Write("<td>Base Value=" & cs2.BaseValue & "<br/>" & _
                         "Counter Frequency=" & cs2.CounterFrequency & "<br/>" & _
                         "Counter Type=" & CounterTypeDesc(cs2.CounterType) & "<br/>" & _
                         "Raw Value=" & cs2.RawValue & "<br/>" & _
                         "System Frequency=" & cs2.SystemFrequency & "<br/>" & _
                         "Time Stamp=" & cs2.TimeStamp & "<br/>" & _
                         "Time Stamp 100n Sec=" & cs2.TimeStamp100nSec & "</td>")
      Response.Write("</tr><tr><td colspan='2'>Calc=" + CounterSample.Calculate(cs1, cs2) & "</td>")
      Response.Write("</tr></table></td>")
      Response.Write("</tr>")
    Next
    Response.Write("</table>")
  End Sub

  Function CounterTypeDesc(ct As PerformanceCounterType) As String
  
    Select Case ct
    case PerformanceCounterType.AverageBase: return "AverageBase"
    case PerformanceCounterType.AverageCount64: return "AverageCount64"
    case PerformanceCounterType.AverageTimer32: return "AverageTimer32"
    case PerformanceCounterType.CounterDelta32: return "CounterDelta32"
    case PerformanceCounterType.CounterDelta64: return "CounterDelta64"
    case PerformanceCounterType.CounterMultiBase: return "CounterMultiBase"
    case PerformanceCounterType.CounterMultiTimer: return "CounterMultiTimer"
    case PerformanceCounterType.CounterMultiTimer100Ns: return "CounterMultiTimer100Ns"
    case PerformanceCounterType.CounterMultiTimer100NsInverse: return "CounterMultiTimer100NsInverse"
    case PerformanceCounterType.CounterMultiTimerInverse: return "CounterMultiTimerInverse"
    case PerformanceCounterType.CounterTimer: return "CounterTimer"
    case PerformanceCounterType.CounterTimerInverse: return "CounterTimerInverse"
    case PerformanceCounterType.CountPerTimeInterval32: return "CountPerTimeInterval32"
    case PerformanceCounterType.CountPerTimeInterval64: return "CountPerTimeInterval64"
    case PerformanceCounterType.ElapsedTime: return "ElapsedTime"
    case PerformanceCounterType.NumberOfItems32: return "NumberOfItems32"
    case PerformanceCounterType.NumberOfItems64: return "NumberOfItems64"
    case PerformanceCounterType.NumberOfItemsHEX32: return "NumberOfItemsHEX32"
    case PerformanceCounterType.NumberOfItemsHEX64: return "NumberOfItemsHEX64"
    case PerformanceCounterType.RateOfCountsPerSecond32: return "RateOfCountsPerSecond32"
    case PerformanceCounterType.RateOfCountsPerSecond64: return "RateOfCountsPerSecond64"
    case PerformanceCounterType.RawBase: return "RawBase"
    case PerformanceCounterType.RawFraction: return "RawFraction"
    case PerformanceCounterType.SampleBase: return "SampleBase"
    case PerformanceCounterType.SampleCounter: return "SampleCounter"
    case PerformanceCounterType.SampleFraction: return "SampleFraction"
    case PerformanceCounterType.Timer100Ns: return "Timer100Ns"
    case PerformanceCounterType.Timer100NsInverse: return "Timer100NsInverse"
    case else: return "none"
    End Select

  End Function

</script>

<form runat="server">

  
</form>

<!--------------------------------------------------------------------------->
<wrox:Footing runat="server"/>
